/*
 * Copyright (c) 2023 huipei.x
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.xphsc.kubernetes.core.executor;

import cn.xphsc.kubernetes.core.query.*;
import cn.xphsc.kubernetes.utils.Asserts;
import io.kubernetes.client.PodLogs;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.*;
import io.kubernetes.client.openapi.models.*;
import org.apache.commons.lang3.StringUtils;

/**
 * {@link }
 * @author <a href="xiongpeih@163.com">huipei.x</a>
 * @description: Create Executor
 * @since 1.0.0
 */
public class CreateNamespaceExecutor<T> extends AbstractExecutor<T> {
    private String createNamespaceName;
    private  T entity;
    private  CoreV1Api  coreV1Api;
    private NamespaceBody namespaceBody;
    private  CreateNamespaceBody createNamespaceBody;
    private AppsV1Api appsv1api;
    private CreateDeploymentBody createDeploymentBody;
    private NamespacedPodBody namespacedPodBody;
    private CreateNamespacedBody createNamespacedBody;
    public CreateNamespaceExecutor(ApiClient client, NamespaceBody namespaceBody, CreateNamespaceMode createNamespaceMode) {
        super(client);
        this.createNamespaceName=createNamespaceMode.name();
        this.coreV1Api=new CoreV1Api(this.getClient());
        this.namespaceBody=namespaceBody;
    }
    public CreateNamespaceExecutor(ApiClient client, CreateNamespaceBody createNamespaceBody, CreateNamespaceMode createNamespaceMode) {
        super(client);
        this.createNamespaceName=createNamespaceMode.name();
        this.coreV1Api=new CoreV1Api(this.getClient());
        this.createNamespaceBody=createNamespaceBody;
    }


    public CreateNamespaceExecutor(ApiClient client, CreateDeploymentBody createDeploymentBody, CreateNamespaceMode createNamespaceMode) {
        super(client);
        this.createNamespaceName=createNamespaceMode.name();
        this.appsv1api=new AppsV1Api(this.getClient());
        this.createDeploymentBody=createDeploymentBody;
    }

    public CreateNamespaceExecutor(ApiClient client, NamespacedPodBody namespacedPodBody, CreateNamespaceMode createNamespaceMode) {
        super(client);
        this.createNamespaceName=createNamespaceMode.name();
        this.coreV1Api=new CoreV1Api(this.getClient());
        this.namespacedPodBody=namespacedPodBody;
    }

    public CreateNamespaceExecutor(ApiClient client, CreateNamespacedBody createNamespacedBody, CreateNamespaceMode createNamespaceMode) {
        super(client);
        this.createNamespaceName=createNamespaceMode.name();
        this.coreV1Api=new CoreV1Api(this.getClient());
        this.createNamespacedBody=createNamespacedBody;
    }
    @Override
    protected  T doExecute() {
        if (StringUtils.isNotBlank(createNamespaceName)) {
            switch (createNamespaceName){
                case "NS":
                    try {
                        entity = (T) coreV1Api.createNamespace(namespaceBody.v1Namespace(),namespaceBody.pretty(), namespaceBody.dryRun(), namespaceBody.fieldManager(),null);
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "SECRET":
                    try {
                        entity = (T) coreV1Api.createNamespacedSecret(createNamespaceBody.namespace(),createNamespaceBody.body(),createNamespaceBody.pretty(),createNamespaceBody.dryRun(),createNamespaceBody.fieldManager(),createNamespaceBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "DEPLY":
                    try {
                        entity = (T) appsv1api.createNamespacedDeployment(createDeploymentBody.namespace(),createDeploymentBody.body(),createDeploymentBody.pretty(),createDeploymentBody.dryRun(),createDeploymentBody.fieldManager(),createDeploymentBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "POD":
                    try {
                        Asserts.notNull(namespacedPodBody.body(),"V1Pod Object cannot be empty" );
                        entity = (T) coreV1Api.createNamespacedPod(namespacedPodBody.namespace(),namespacedPodBody.body(),namespacedPodBody.pretty(),namespacedPodBody.dryRun(),namespacedPodBody.fieldManager(),null);
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "BINDING":
                    try {
                        Asserts.isInstanceOf(V1Binding.class,createNamespacedBody.body());
                        Asserts.notNull(createNamespacedBody.body(),"V1Binding Object cannot be empty" );
                        entity = (T) coreV1Api.createNamespacedBinding(createNamespacedBody.namespace(), (V1Binding) createNamespacedBody.body(),createNamespacedBody.pretty(),createNamespacedBody.dryRun(),createNamespacedBody.fieldManager(),null);
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "CONFIGMAP":
                    try {
                        Asserts.isInstanceOf(V1ConfigMap.class,createNamespacedBody.body());
                        Asserts.notNull(createNamespacedBody.body(),"V1ConfigMap Object cannot be empty" );
                        entity = (T) coreV1Api.createNamespacedConfigMap(createNamespacedBody.namespace(), (V1ConfigMap) createNamespacedBody.body(),createNamespacedBody.pretty(),createNamespacedBody.dryRun(),createNamespacedBody.fieldManager(),createNamespacedBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "ENDPOINTS":
                    try {
                        Asserts.isInstanceOf(V1Endpoints.class,createNamespacedBody.body());
                        Asserts.notNull(createNamespacedBody.body(),"V1Endpoints Object cannot be empty" );
                        entity = (T) coreV1Api.createNamespacedEndpoints(createNamespacedBody.namespace(), (V1Endpoints) createNamespacedBody.body(),createNamespacedBody.pretty(),createNamespacedBody.dryRun(),createNamespacedBody.fieldManager(),createNamespacedBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "PODTEMPLATE":
                    try {
                        Asserts.isInstanceOf(V1PodTemplate.class,createNamespacedBody.body());
                        Asserts.notNull(createNamespacedBody.body(),"V1PodTemplate Object cannot be empty" );
                        entity = (T) coreV1Api.createNamespacedPodTemplate(createNamespacedBody.namespace(), (V1PodTemplate) createNamespacedBody.body(),createNamespacedBody.pretty(),createNamespacedBody.dryRun(),createNamespacedBody.fieldManager(),createNamespacedBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "SERVICE":
                    try {
                        Asserts.isInstanceOf(V1Service.class,createNamespacedBody.body() );
                        Asserts.notNull(createNamespacedBody.body(),"V1Service Object cannot be empty" );
                        entity = (T) coreV1Api.createNamespacedService(createNamespacedBody.namespace(), (V1Service) createNamespacedBody.body(),createNamespacedBody.pretty(),createNamespacedBody.dryRun(),createNamespacedBody.fieldManager(),createNamespacedBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "SERVICEACCOUNT":
                    try {
                        Asserts.isInstanceOf(V1ServiceAccount.class,createNamespacedBody.body());
                        Asserts.notNull(createNamespacedBody.body(),"V1ServiceAccount Object cannot be empty" );
                        entity = (T) coreV1Api.createNamespacedServiceAccount(createNamespacedBody.namespace(), (V1ServiceAccount) createNamespacedBody.body(),createNamespacedBody.pretty(),createNamespacedBody.dryRun(),createNamespacedBody.fieldManager(),createNamespacedBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "PERSISTENTVOLUME":
                    try {
                        Asserts.isInstanceOf(V1PersistentVolume.class,createNamespacedBody.body());
                        Asserts.notNull(createNamespacedBody.body(),"V1PersistentVolume Object cannot be empty" );
                        entity = (T) coreV1Api.createPersistentVolume((V1PersistentVolume) createNamespacedBody.body(),createNamespacedBody.pretty(),createNamespacedBody.dryRun(),createNamespacedBody.fieldManager(),createNamespacedBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "NODE":
                    try {
                        Asserts.isInstanceOf(V1Node.class,createNamespacedBody.body());
                        Asserts.notNull(createNamespacedBody.body(),"V1Node Object cannot be empty" );
                        entity = (T) coreV1Api.createNode((V1Node) createNamespacedBody.body(),createNamespacedBody.pretty(),createNamespacedBody.dryRun(),createNamespacedBody.fieldManager(),createNamespacedBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "DAEMONSET":
                    try {
                        Asserts.isInstanceOf(V1DaemonSet.class,createNamespacedBody.body());
                        Asserts.notNull(createNamespacedBody.body(),"V1DaemonSet Object cannot be empty" );
                        entity = (T) appsv1api.createNamespacedDaemonSet(createNamespacedBody.namespace(), (V1DaemonSet) createNamespacedBody.body(),createNamespacedBody.pretty(),createNamespacedBody.dryRun(),createNamespacedBody.fieldManager(),createNamespacedBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "REPLICASET":
                    try {
                        Asserts.isInstanceOf(V1ReplicaSet.class,createNamespacedBody.body());
                        Asserts.notNull(createNamespacedBody.body(),"V1ReplicaSet Object cannot be empty" );
                        entity = (T) appsv1api.createNamespacedReplicaSet(createNamespacedBody.namespace(), (V1ReplicaSet) createNamespacedBody.body(),createNamespacedBody.pretty(),createNamespacedBody.dryRun(),createNamespacedBody.fieldManager(),createNamespacedBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "STATEFULSET":
                    try {
                        Asserts.isInstanceOf(V1StatefulSet.class,createNamespacedBody.body());
                        Asserts.notNull(createNamespacedBody.body(),"V1StatefulSet Object cannot be empty" );
                        entity = (T) appsv1api.createNamespacedStatefulSet(createNamespacedBody.namespace(), (V1StatefulSet) createNamespacedBody.body(),createNamespacedBody.pretty(),createNamespacedBody.dryRun(),createNamespacedBody.fieldManager(),createNamespacedBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                default:

            }
        }
        return (T) entity;
    }
    public static enum CreateNamespaceMode {
        /**
         *
         */
        NS,
        SECRET,
        DEPLY,
        POD,
        PODBINDING,
        ASYNC,
        BINDING,
        CONFIGMAP,
        ENDPOINTS,
        PODTEMPLATE,
        SERVICE,
        SERVICEACCOUNT,
        PERSISTENTVOLUME,
        DAEMONSET,
        REPLICASET,
        STATEFULSET,
        NODE,
        AUTO;
        private CreateNamespaceMode() {

        }
        private CreateNamespaceMode(String name) {
            this.name=name;
        }
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}


